package club.baimahu.pencil;

import club.baimahu.pencil.domain.entity.CommonBuyer;
import club.baimahu.pencil.domain.entity.Goods;
import club.baimahu.pencil.domain.entity.account.CashAccount;
import club.baimahu.pencil.domain.entity.account.SimpleAccountManager;
import club.baimahu.pencil.domain.session.SimpleGoodFactory;
import club.baimahu.pencil.domain.session.Supermarket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * 模拟消费者进入超市购买商品的场景
 * 1.卖家,卖家都需要自己实例化,商品则通过工厂模式获得
 * 2.买家账户余额不足时抛出BuyException
 */
public class Story1 {
    static Logger logger = LoggerFactory.getLogger(Story1.class);


    public static void main(String[] args) {
        CommonBuyer buyer = new CommonBuyer("小刚");
        buyer.addCacheAccount("622230011229999",new BigDecimal(100));

        Supermarket supermarket = new Supermarket("联华超市-上海分店",new SimpleGoodFactory());
        supermarket.setAccountManager(new SimpleAccountManager(supermarket.getName(),new CashAccount(supermarket.getName(),new BigDecimal(1000))));

        buyer.startShopping(supermarket);

        //顾客想要查看某样商品时，通过工厂模式创建
        Goods goods = supermarket.getGoods("NO001");
        logger.info("商品编号={}，商品名称={}",goods.getGoodsNo(),goods.getName());

        buyer.buy("NO001",supermarket);
        buyer.buy("NO002",supermarket );
        buyer.buy("NO003" ,supermarket);

        for(int j = 0 ; j <20 ; j++){
            buyer.buy("NO002",supermarket);
        }

        buyer.leave(supermarket);

    }
}
